﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;
using MyCommerce.Presentation.GeneralParameters.IViews;
using MyCommerce.Presentation.GeneralParameters.Presenters;
using Innova.FoundationFramework.DataTypes;

namespace MyCommerce.Windows.GeneralParameters.Views
{
    public partial class CategoryFillView : BaseForm, ICategoryFill
    {
        private CategoryFillPresenter pPresenter;

        public CategoryFillView()
        {
            InitializeComponent();
            pPresenter = new CategoryFillPresenter(this);
        }

        #region IEmployeeFillView Members

        public long IdItemEdit { get; set; }

        public OptionSelect OptionViewSelect { get; set; }

        public List<Classifier> ColCategory
        {
            set
            {
                cmbCategory.DisplayMember = "Name";
                cmbCategory.ValueMember = "IdClassifier";
                cmbCategory.DataSource = value;
            }
        }

        public List<Classifier> ColProductType
        {
            set
            {
                treeProductTypes.SubLevelCheckBox = true;
                treeProductTypes.ShowSubLevelCheckBox = false;
                treeProductTypes.SetNodes<Classifier>(value, "Name", "IdClassifier", "");
            }
        }

        public List<CategoryRelation> Get()
        {
            List<CategoryRelation> colCategoryRelation = new List<CategoryRelation>();
            foreach (long item in treeProductTypes.GetSelectedEntitiesIdList())
            {
                CategoryRelation objCategoryRelation = new CategoryRelation();
                objCategoryRelation.IdcCategory = (long)cmbCategory.SelectedValue;
                objCategoryRelation.IdcProductType = item;
                colCategoryRelation.Add(objCategoryRelation);
            }
            
            return colCategoryRelation;
        }

        public void Set(List<CategoryRelation> colToLoad)
        {
            if (colToLoad != null)
	        {
                if (String.IsNullOrEmpty(cmbCategory.ValueMember) == false
                       && colToLoad[0].IdcCategory > 0)
                {
                    cmbCategory.SelectedValue = colToLoad[0].IdcCategory;
                    cmbCategory.Enabled = false;
                }
                List<long> idsProductType = new List<long>();
                foreach (CategoryRelation item in colToLoad)
                {
                    idsProductType.Add(item.IdcProductType);
                }
                treeProductTypes.SetSelectedEntitiesIdList(idsProductType);
	        }
        }

        public void CloseAndSetResult(bool result)
        {
            if (result)
                Context.ShowInformationMessageBox("La información se guardó correctamente.", "Creación/Edición de categorias.");
            Result = result;
            Close();
        }

        public event GenericEventHandler<List<CategoryRelation>> OnLoadView;

        public event EventHandler OnSave;

        public event EventHandler OnCancel;

        #endregion

        private bool ValidateView()
        {
            bool vResult = true;
            StringBuilder message = new StringBuilder();

            if (treeProductTypes.GetSelectedEntitiesIdList().Count() == 0)
            {
                vResult = false;
                message.AppendLine("Debe seleccionar al menos un tipo de producto.");
            }

            //if (string.IsNullOrEmpty(fieldName.Text.Trim()))
            //{
            //    vResult = false;
            //    message.AppendLine("Debe ingresar el nombre del empleado.");
            //}
            
            if (!vResult)
            {
                Context.ShowErrorMessageBox(message.ToString(), "Validación de datos.");
            }

            return vResult;
        }

        private void EmployeeFillView_Load(object sender, EventArgs e)
        {
            List<CategoryRelation> objProduct = null;
            OptionViewSelect = OptionSelect.Create;
            if (Parameter != null)
            {
                objProduct = (List<CategoryRelation>)Parameter;
                OptionViewSelect = OptionSelect.Edit;
            }
            if (OnLoadView != null)
                OnLoadView(this, new GenericEventArgs<List<CategoryRelation>>(objProduct));
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (OnCancel != null)
                OnCancel(this, new EventArgs());
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateView())
                {
                    if (OnSave != null)
                        OnSave(this, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }
    }
}
